package com.hspedu.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.hspedu.entity.Member;
import com.hspedu.entity.Result;
import com.hspedu.handler.CustomGlobalBlockHandler;
import com.hspedu.handler.CustonGlobalFallbackHandler;
import com.hspedu.service.MemberService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
public class MemberController {

    @Resource
    private MemberService memberService;
    private static int num=0;
    @GetMapping("/news1")
    @SentinelResource(value = "news1",
            blockHandlerClass = CustomGlobalBlockHandler.class,
            blockHandler = "handleMethod1")
    public Result queryNews1(@RequestParam(value = "id",required = false)String id,
                             @RequestParam(value = "type",required = false)String type){

        return Result.Success("返回id="+id+"新闻from DB");
    }
    @GetMapping("/t6")
    @SentinelResource(value = "t6",
            blockHandlerClass = CustomGlobalBlockHandler.class,
            blockHandler = "handleMethod2",
            fallbackClass = CustonGlobalFallbackHandler.class,
            fallback = "fallbackHandler1",
            exceptionsToIgnore = {NullPointerException.class})
    public Result t6(){
        if(++num%5==0){
            throw new RuntimeException("num异常"+num);
        }
        if (num%7==0){
            throw new NullPointerException("空指针异常"+num);
        }
        log.info("线程id-{},时间time-{}",Thread.currentThread().getId(),new Date());
        return Result.Success("t6()执行...");
    }
    @GetMapping("/news")
    @SentinelResource(value = "news",blockHandler = "newsBlockHandler")
    public Result queryNews(@RequestParam(value = "id",required = false)String id,
                            @RequestParam(value = "type",required = false)String type){

        return Result.Success("返回id="+id+"新闻from DB");
    }
    public Result newsBlockHandler(String id, String type, BlockException e){
        return Result.Success("查询id="+id+"新闻 触发热点key限流保护 sorry...");
    }
    @GetMapping("/t1")
    public Result T1(){
        return Result.Success("t1()执行...");
    }
    @GetMapping("/t2")
    public Result T2(){
        //try {
        //    TimeUnit.SECONDS.sleep(1);
        //} catch (InterruptedException e) {
        //    throw new RuntimeException(e);
        //}
        log.info("线程id-{},时间time-{}",Thread.currentThread().getId(),new Date());
        return Result.Success("t2()执行...");
    }
    @GetMapping("/t3")
    public Result T3(){
        try {
            TimeUnit.MILLISECONDS.sleep(300);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        log.info("线程id-{},时间time-{}",Thread.currentThread().getId(),new Date());
        return Result.Success("t3()执行...");
    }
    @GetMapping("/t4")
    public Result T4(){
        if (++num%2==0){
            System.out.println(3/0);
        }

        return Result.Success("t4()执行...");
    }
    @GetMapping("/t5")
    public Result T5(){
        if (++num <= 10){
            System.out.println(3/0);
        }
        return Result.Success("t5()执行...");
    }


    @GetMapping("/member/getMemberById")
    public Result getMemberById(@RequestParam("id")Long id){
        //String color = request.getParameter("color");
        //try {
        //    TimeUnit.SECONDS.sleep(2);
        //} catch (InterruptedException e) {
        //    throw new RuntimeException(e);
        //}
        Member member = memberService.queryById(id);
        if (member!=null){
            return Result.Success("查询会员成功，10004服务方",member);
        }else {
            return Result.Error("402","id="+id+"不存在");
        }

    }

    @PostMapping("/member/save")
    public Result save(@RequestBody Member member){
        log.info("member-service-provider.menber={}",member);
        int save = memberService.save(member);
        if (save>0){
            return Result.Success("添加会员成功",save);
        }else {
            return Result.Error("401","添加会员失败");
        }

    }


}
